package com.bibrApp.difu.adapter;


import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.HomeMarketItemBean;
import com.bibrApp.difu.utils.StringSplitUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


public class MarketAdapter extends RecyclerView.Adapter<MarketAdapter.MarketItemHolder> {
    private List<HomeMarketItemBean> datas = new ArrayList<>();
    private Context mContext;

    public MarketAdapter(Context context) {
        this.mContext = context;
    }

    public void setNewData(List<HomeMarketItemBean> datas){
        this.datas.clear();
        if(datas != null){
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }
    public void setItemDatas(List<HomeMarketItemBean> datas , int pos) {
        this.datas.clear();
        this.datas.addAll(datas);
        notifyItemChanged(pos);
    }
    @NonNull
    @Override
    public MarketItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_home,parent,false);
        return new MarketItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MarketItemHolder holder, int position) {
        HomeMarketItemBean bean = datas.get(position);
        if(!TextUtils.isEmpty(bean.getTradePairEunit())){
            String[] splitName = StringSplitUtil.SpiltString2Array(bean.getTradePairEunit());
            if(splitName != null && splitName.length > 1){
                String bNameOfChange = splitName[1] + "/" + splitName[0];
                SpannableString styledText = new SpannableString(bNameOfChange);
                styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style_text_left), 0, splitName[1].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                styledText.setSpan(new TextAppearanceSpan(mContext, R.style.style_text_right), splitName[1].length(), bNameOfChange.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.nameTV.setText(styledText);
            }
        }

        holder.dollarTV.setText(String.valueOf(bean.getNewPrice()));
        holder.rmbTV.setText(bean.getNewPrice()+"CNY");

        if(bean.getQuoteChange() < 0){
            holder.rateTV.setBackgroundResource(R.drawable.shape_red_button);
            holder.rateTV.setText(bean.getQuoteChange()+"%");
            holder.rateTV.setTextColor(mContext.getResources().getColor(R.color.white));
            holder.rmbTV.setTextColor(mContext.getResources().getColor(R.color.home_rank_item_red_color));
            holder.dollarTV.setTextColor(mContext.getResources().getColor(R.color.home_rank_item_red_color));
        }else if(bean.getQuoteChange() > 0){
            holder.rateTV.setBackgroundResource(R.drawable.shape_green_button);
            holder.rateTV.setText("+"+bean.getQuoteChange()+"%");
            holder.rateTV.setTextColor(mContext.getResources().getColor(R.color.white));
            holder.rmbTV.setTextColor(mContext.getResources().getColor(R.color.home_rank_item_green_color));
            holder.dollarTV.setTextColor(mContext.getResources().getColor(R.color.home_rank_item_green_color));
        }else{
            holder.rateTV.setText(bean.getQuoteChange()+"%");
            holder.rateTV.setTextColor(mContext.getResources().getColor(R.color.black_9));
            holder.rateTV.setBackgroundResource(R.drawable.shape_gray);
            holder.rmbTV.setTextColor(mContext.getResources().getColor(R.color.black_9));
            holder.dollarTV.setTextColor(mContext.getResources().getColor(R.color.black_3));
        }
        /*holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                gotoKLine( bean.getTradePairEunit());
            }
        });
        holder.rateTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                gotoKLine( bean.getTradePairEunit());
            }
        });*/
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }


class MarketItemHolder extends RecyclerView.ViewHolder {
        TextView nameTV;
        TextView dollarTV;
        TextView rmbTV;
        Button rateTV;
        LinearLayout ll;

        public MarketItemHolder(@NonNull View itemView) {
            super(itemView);
            ll = itemView.findViewById(R.id.item_ll);
            nameTV = itemView.findViewById(R.id.item_home_name);
            dollarTV = itemView.findViewById(R.id.item_homet_dollar);
            rmbTV = itemView.findViewById(R.id.item_home_rmb);
            rateTV = itemView.findViewById(R.id.item_home_rate);

        }
    }
}
